import com.jfinal.aop.Interceptor;
import com.jfinal.aop.Invocation;
import com.jfinal.core.Controller;
import com.xinxing.annotation.EmptyInterface;
import com.xinxing.utils.StampToDate;

/**
 * 方法不能为空注解验证
 * @author Administrator
 *
 */
public class EmptyInterceptor implements Interceptor {

	@Override
	public void intercept(Invocation inv) {
		Annotation annotation = inv.getMethod().getAnnotation(Annotation.class);
        //如果有使用注解就调用验证方法，如果没有就继续继续往下往下运行
		if (annotation != null) {
			noEmpty(annotation, inv);
        }else{
        	inv.invoke();
        }
	}
	
	public void noEmpty(Annotation annotation,Invocation inv) {
		Controller con = inv.getController();
		
        String header = con.getHeader("Content-Type"); //取出head头
        if (header != null && header.indexOf("multipart/form-data") != -1) { //判断是否是form-data
            //form-data模式需要根据自己的情况修改下
        	String folderName = "loss/" + StampToDate.getMonthDay();
        	con.getFile("photo",folderName);
        }
        String[] value = annotation.value();
        boolean isTrue = true;
        String key = "";
        for (String v : value) {
            String parameter = con.getPara(v);
            if (parameter == null || parameter.trim().length() == 0) {
            	isTrue = false;
            	key = v;
            	break;
            }
        }
        if(isTrue){
        	inv.invoke();
        }else{
        	con.setAttr("msg", key+"不能为空");
        	con.setAttr("code", "509");
        	con.renderJson();
        }
        
    }
}
